package com.zmn.oms.third.taobao.service.impl;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderDutyConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.taobao.TaobaoTmcConstants;
import com.zmn.oms.third.taobao.service.TmallService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 类描述：向天猫B端安装推送订单变化通知的服务
 *
 * @author LiangHailong
 * @date 2019/09/22 14:32
 */
@Service("tmallOrderChangeNotifyService")
@Slf4j
public class TmallOrderChangeNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    private TmallService tmallService;

    @Override
    @SuppressWarnings("all")
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {
        if (!TaobaoTmcConstants.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", TaobaoTmcConstants.NAME_TMALL);
            return;
        }
        // 管道订单不处理
        if (Objects.equals(OrderConsts.CHANNEL_ID_TMALL_PIPE, orderWorkLogChange.getChannelId())) {
            return;
        }

        int logType = orderWorkLogChange.getLogType().intValue();

        // 修改预约时间
        if (isChangeDutyTime(logType, orderWorkLogChange)) {
            if (!Objects.equals(GlobalConsts.OPERATE_USER_TYPE_CHANNEL, orderWorkLogChange.getOperatorType())) {
                tmallService.syncDutyTime(orderWorkLogChange);
            }
        }
        // 修改预约失败
        else if(isChangeDutyTimeFailed(logType, orderWorkLogChange)){
            tmallService.syncReserveFailed(orderWorkLogChange);
        }
        // 挂起
        else if(OrderLogConsts.ORDER_LOG_TYPE_ORDER_SUSPEND == logType){
            tmallService.syncHangUp(orderWorkLogChange);
        }
        // 派单
        else if (OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE == logType) {
            tmallService.syncMaster(orderWorkLogChange);
            // 工程师派单以后，2小时内需要同步一次预约时间，天猫用于考核预约及时率
            if (Objects.equals(OrderConsts.CHANNEL_ID_TMALL_WYG, orderWorkLogChange.getChannelId())) {
                tmallService.syncDutyTime(orderWorkLogChange);
            }
        }
        // 取消、失败、弃单
        else if (OrderLogConsts.ORDER_LOG_TYPE_CANCEL == logType || OrderConsts.ORDER_OP_TYPE_FAIL == logType
                || OrderConsts.ORDER_OP_TYPE_DISCARD == logType) {
            tmallService.syncOrderCancel(orderWorkLogChange);
        }
        // 完成
        else if (OrderLogConsts.ORDER_LOG_TYPE_COMPLETE == logType
                || OrderLogConsts.ORDER_LOG_TYPE_SERVICE_COMPLETE == logType) {
            tmallService.syncOrderComplete(orderWorkLogChange);
        }

    }

    // 修改预约失败
    private boolean isChangeDutyTimeFailed(int logType, OrderWorkLogChange orderWorkLogChange) {
        if (OrderLogConsts.ORDER_LOG_TYPE_UPDATE == logType
                || OrderLogConsts.ORDER_LOG_TYPE_CONFIRM == logType
                || OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME == logType
                || OrderLogConsts.ORDER_LOG_TYPE_MODIFY_DUTY_TIME == logType) {
            Date dutyTime = orderWorkLogChange.getDutyTime();
            // 修改成预约失败
            Integer dutyStatus = orderWorkLogChange.getDutyStatus();
            if (Objects.isNull(dutyTime) && Objects.equals(OrderDutyConsts.DUTY_STATUS_FAIL, dutyStatus)) {
                return true;
            }
        }
        return false;
    }

}
